/**
 * Created by trisatria on 2/21/14.
 */
var my_pro_page = 1;
var is_suppend = 0;
function my_render(data,div){
    console.log(data['images']);
    var content_cat = '';
    if(data['tocatid']!= null && data['tocatid'] != ''){
        content_cat = _topic_cat[parseInt(data['tocatid']) - 1]['title'];
    }
    var content = ' <li id="item_id_'+data['topi']+'"  class=" ui-li-has-thumb"><a href="'+data['topi']+'"  data-rel="popup" data-position-to="window" data-transition="pop" class="btn_option ui-btn"><img style="top:23px" src="' + _host_img+ "/" +data['images']+'"><h2>'+data['title']+'</h2><p>'+_topic_type[data['topic_type']]+' :: '+_topic_loc[data['topic_location']]+':: '+content_cat+'</p><p>'+ data['create_date']+' <b> '+ $.timeago(data['create_date'])+'</b></p>';
    if(data['show_price'] == 1){
        content += '<p class="price">'+data['price']+'VNĐ </p>';
    }
    else{
        content += '<p>Liên Hệ</p>';
    }
    content +='</a>';
    content+= '</li>';
    $(div).append(content);
    $('#myproduct .price').formatCurrency($.formatCurrency.regions['vi-VN']);
}
$(document).on('pageshow','#myproduct', function(event) {
    init_search();
    //$.mobile.loading( "show");
    checklogin();
    //init location
    for(var i=0; i< _topic_loc.length ; i = parseInt(i) + 1){
        var content = '<option value="'+i+'" >'+_topic_loc[i]+'</option>';
        $('.select_loc').append(content);

    }
    //init cat
    for(var i=0; i< _topic_cat.length ; i = parseInt(i) + 1){
        console.log(_topic_cat[i]);
        var content = '<option value="'+_topic_cat[i]['id']+'" >'+_topic_cat[i]['title']+'</option>';
        $('.select_cat').append(content);
    }
    load_product(_host+'topic/index'+'?user='+user_id);
    $('#my_pro_load_more').attr('data_href', _host+'topic/index'+'?user='+user_id);
    //btn-load-more-click
    $('#my_pro_load_more').click(function(){
       var href = $(this).attr('data_href');
        my_pro_page = parseInt(my_pro_page) + 1;
        load_product(href);
    });

});
function load_product(url){
    //var herf = $(this).attr('href');
    //$.mobile.loading('show');
    _url = url;
    $.ajax({
        url: _url,
        type: 'GET',
        data: {title: product_name, cat: select_cat, loc: select_loc, type:select_type, page:my_pro_page }
    }).done(function(data){
        var jdata = $.parseJSON(data);
        //console.log(jdata);
        if(jdata.length != 0){
            for(var i = 0 ; i< jdata.length ; i= parseInt(i) + 1){
                console.log(jdata[i]);
                my_render(jdata[i],'#my_product_ajax_table');
            }
        }
        else{
            $('.no-topic-info').remove();
            $('#my_product_ajax_table').append('<li class="ui-li-has-thumb no-topic-info" style="padding-top: 15px; padding-bottom: 15px;">Không Còn Tin Nào</li>');
        }

      //  $.mobile.loading( "hide");
    });
    return false;
}
$(document).on('click','.btn_option', function(){
    var id = $(this).attr('href');
    $('#show_option').popup('open');
    $('#show_option').children('#view').attr('href','productdetail.html?product_id='+id);
    $('#show_option').children('#edit').attr('href','editproduct.html?product_id='+id);
    $('#show_option').children('#delete').attr('href',id);
    console.log(id);
    return false;
});
$(document).on('click','#delete', function(){
    var href = $(this).attr('href');
    $.ajax({
        url: _host+'topic/delete',
        type: 'post',
        data:{product_id: href }
    }).done(function(data){
        if(data!= '-1'){

            $('#show_option').popup('close');
            $('#item_id_'+data).remove();

        }
    });
    return false;
});
$(document).on('click','#btn_normal', function(){
    is_suppend = 0;
    my_pro_page = 1;
    $('#my_product_ajax_table').empty();
    load_product(_host+'topic/index'+'?user='+user_id);
    $('#my_pro_load_more').attr('data_href', _host+'topic/index'+'?user='+user_id);
});
$(document).on('click','#btn_pending', function(){
    is_suppend = 1;
    my_pro_page = 1;
    $('#my_product_ajax_table').empty();
    load_product(_host+'topic/gettopicsuspend?user_id='+user_id);
    $('#my_pro_load_more').attr('data_href', _host+'topic/gettopicsuspend?user_id='+user_id);
})


$(window).on( "scrollstop", function( event ) {
    //alert('scrolltop: '+ $(window).scrollTop() + 'dcHeght: ' + $(document).height() + ' windowHeight ' + $(window).height());
    if($(document).height() > $(window).height())
    {
        if( ($(window).scrollTop() / ($(document).height() - $(window).height())) >= 0.5){
            var activePage = $.mobile.activePage[0].id;
            //alert(activePage)
            if(activePage == 'myproduct'){
                //$.mobile.loading( "show");
                if(is_suppend == 1){
                    //alert('1');
                    my_pro_page = parseInt(my_pro_page) + 1;
                    load_product(_host+'topic/gettopicsuspend?user_id='+user_id);

                  }
                  else{
                      my_pro_page = parseInt(my_pro_page) + 1;
                      load_product(_host+'topic/index'+'?user='+user_id);
                      //$('#my_pro_load_more').attr('data_href', _host+'topic/gettopicsuspend?user_id='+user_id);
                  }
            }
        }
    }
} )
